﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Web;
using WoBian.Lib.ConstString;
using WoBian.Common;
namespace WoBian.ServiceModel
{
    public class MyApplicationContext
    {
        public string UserId
        {
            get
            {
                return Guid.NewGuid().ToString("D");
                //if (HttpContext.Current.Request.Cookies[SessionKey.UserId] == null)
                //    throw new Exception("用户信息已经失效!请重新登录!");
                //return HttpContext.Current.Request.Cookies[SessionKey.UserId].ToNullableString();
            }
            set
            {
                HttpContext.Current.Response.Cookies.Add(new HttpCookie(SessionKey.UserId, value));
            }
        }

        public static MyApplicationContext Current
        {
            get { return new MyApplicationContext(); }
        }

        public string ValidateCode
        {
            get
            {
                if (HttpContext.Current.Request.Cookies[SessionKey.ValidateCode] == null)
                    throw new Exception("用户信息已经失效!请重新登录!");
                return HttpContext.Current.Request.Cookies[SessionKey.ValidateCode].ToNullableString();
            }
            set
            {
                HttpContext.Current.Response.Cookies.Add(new HttpCookie(SessionKey.ValidateCode, value));
            }
        }

        public string NickName
        {
            get
            {
                if (HttpContext.Current.Request.Cookies[SessionKey.NickName] == null)
                    throw new Exception("用户信息已经失效!请重新登录!");
                return HttpContext.Current.Request.Cookies[SessionKey.NickName].ToNullableString();
            }
            set 
            {
                HttpContext.Current.Response.Cookies.Add(new HttpCookie(SessionKey.NickName,value));
            }
        }

        public string Name
        {
            get
            {
                if (HttpContext.Current.Request.Cookies[SessionKey.Name] == null)
                    throw new Exception("用户信息已经失效!请重新登录!");
                return HttpContext.Current.Request.Cookies[SessionKey.Name].ToNullableString();
            }
            set
            {
                HttpContext.Current.Response.Cookies.Add(new HttpCookie(SessionKey.Name,value));
            }
        }

        public string[] Roles
        {
            get
            {
                if (HttpContext.Current.Request.Cookies[SessionKey.Roles] == null)
                    throw new Exception("用户信息已经失效!请重新登录!");
                return HttpContext.Current.Request.Cookies[SessionKey.Roles].ToNullableString().Split('#');
            }
            set
            {
               HttpContext.Current.Response.Cookies.Add(new HttpCookie(SessionKey.Name,string.Join("#", value)));
            }
        }
         
    }
}
